<?php 
namespace app\admin\controller;

use think\Controller;
use think\Db;
use think\Request;
		
class Sowingmap extends cmsBase {
	public function index(){
		$data = Db::name('cms_broadcast')->where('status','not in','2')->order('id','desc')->paginate(20);
		$arr = [
            0 => '首页轮播图',
            1 => '水平广告图片',
            2 => '底部广告图',
		];
		$page = $data->render();
		return view('sowingmap/index',['arr'=>$arr,'data'=>$data,'page'=>$page]);
	}

	public function addphoto(){
		if(empty($_POST)){
			$arr = [
                0 => '首页轮播图',
                1 => '水平广告图片',
                2 => '底部广告图',
			];
			return view('sowingmap/addphoto',['arr'=>$arr]);
		}else{
		$param = $this->request->post();
		$file = $this->request->file('img');
		        // dump($_POST);die;
		$data=[
			'name' => $param['name'],
			'Administrators'  => 1,//session获取
			'url' => $param['url'],
			'type' => $param['type'],
			'createtime' => date('Y-m-d H:i:s'),
			'status' => $param['status']
		];
        $info = $file->validate(['size'=>3300*1000,'ext'=>'jpg,png,gif'])->move(ROOT_PATH . 'public' . DS . 'upload/sowingmap');

            if($info){
            //成功上传后 获取上传信息
            //输出 jpg
                $info->getExtension();
            //输出 42a79759f284b767dfcb2a0197904287.jpg
                $img=$info->getFilename();
                $data['photo'] = date('Ymd').'/'.$img;

                $s = Db::name('cms_broadcast')->insert($data);
                if($s){
                    return "<script>alert('新增成功');parent.location.reload();</script>";
                }else{
                    return "<script>alert('添加失败');history.go(-1);</script>";
                }
            }else{
                return "<script>alert('图片上传失败，请检查图片大小');history.go(-1);</script>";
            }
		}
	}

	public function openAndoffphoto(){
		$photo = Db::name('cms_broadcast')->where('id',$_GET['id'])->find();
		if($photo){
			if($photo['status']==0){
				$up = Db::name('cms_broadcast')->where(['id'=>$_GET['id']])->update(['status'=>1]);
			}elseif($photo['status']==1){
				$up = Db::name('cms_broadcast')->where(['id'=>$_GET['id']])->update(['status'=>0]);
			}
			if($up){
                return "<script>window.location.href = document.referrer;</script>";
			}else{
                return "<script>alert('修改失败');history.go(-1);</script>";
			}
		}else{
            return "<script>alert('修改失败');history.go(-1);</script>";
		}
	}

	public function photodel(){
		$photo = Db::name('cms_broadcast')->where('id',$_POST['id'])->find();
		if($photo){
			$del = Db::name('cms_broadcast')->where(['id'=>$_POST['id']])->update(['status'=>2]);
			if($del){
				return 'success';
			}else{
				return 'error';
			}
		}else{
			return 'error';
		}
	}
}